<?php
/**
 +------------------------------------------------------------------------------
 * 招商会展位售卖
 +------------------------------------------------------------------------------
 * @version   2.0
 * @since 2016-4-18
 +------------------------------------------------------------------------------
 */
namespace Admin\Controller;

class BusinessController extends AdminController {
    
    
    /**
     * @todo 品类管理
     * @author fzh
     */
    public function index(){
        $search_text = trim(I('search_text','','strip_tags'));//搜索内容
        $map = array();//初始化查询条件
        //查询条件
        if(!empty($search_text)){
            $map['business_category_name'] = array('like', '%'.(string)$search_text.'%');
        }
        //列表
        $map['status']=array('gt',-1);
        $field = 'business_category_id,business_category_name,sort,status';
        $order = 'sort desc, business_category_id asc';
        $list = $this->lists('business_category',$map,$order,$field);
        $this->assign('_list',$list);
        $this->assign('meta_title','品类管理');
        cookie('__forward__',$_SERVER['REQUEST_URI']);
        $this->display('index');
    }
    
    /**
     * @todo 添加品类
     * @author fzh
     */
    public function add(){
        if(IS_POST){
            $this->update();
        }else{
            $this->assign('list',null);
            $this->assign('meta_title','新增品类');
            $this->assign('meta_title_c','品类管理');
            $this->display('add');
        }
    }
    
    /**
     * @todo 编辑品类
     * @author fzh
     */
    public function edit(){
        if(IS_POST){
            $this->update();
        }else{
            //编辑时，品类详细信息
            $id = (int)I('id');
            if($id == 0){
                $this->error('请正确操作');
            }
            $info = D('BusinessCategory')->businessCategoryShow($id,'business_category_id,business_category_name,sort');
            if(empty($info)){
                $this->error('品类信息不存在');
            }
            //显示
            $this->assign('list',$info);
            $this->assign('meta_title','更新品类');
            $this->assign('meta_title_c','品类管理');
            $this->display('add');
        }
    }
    
    /**
     * @todo 添加/编辑品类(仅供内部调用)
     * @author fzh
     */
    private function update(){
        $business_category_id = (int)I('business_category_id');
        $obj = D('BusinessCategory');
        $id = $obj->update();
        if(empty($id)){
            $this->error($obj->getError());
        }
        //记录行为
        if($business_category_id == 0){
            action_log('business_category_add', 'business_category', $id, UID);
        }else{
            action_log('business_category_edit', 'business_category', $business_category_id, UID);
        }
        $this->success('操作成功',cookie('__forward__'));
    }
    
    /**
     * @todo 品类状态修改
     * @param string $method 标识
     * @author fzh
     */
    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if(empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $map[D('BusinessCategory')->getPk()] = array('in',$id);
        switch ( strtolower($method) ){
//             case 'forbiduser':
//                 $this->forbid('BusinessCategory', $map );
//                 break;
//             case 'resumeuser':
//                 $this->resume('BusinessCategory', $map );
//                 break;
            case 'deleteuser':
                action_log('business_category_del', 'business_category', $id, UID);
                $this->delete('BusinessCategory', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }
    
    /**
     * @todo 招商会店铺（所有）
     * @author fzh
     */
    public function shop(){
        $search_text = trim(I('search_text','','strip_tags'));//搜索内容
        $activity_id = (int)I('activity_id');
        $business_category_id = (int)I('business_category_id');
        $map = array();//初始化查询条件
        //查询条件
        if(!empty($search_text)){
            $map['business_shop_name'] = array('like', '%'.(string)$search_text.'%');
        }
        if($activity_id>0){
            $map['activity_id'] = $activity_id;
        }
        if($business_category_id>0){
            $map['business_category_id'] = $business_category_id;
        }
        //列表
        $map['status']=array('gt',-1);
        $list = $this->lists('business_shop',$map,'business_shop_id desc');
        int_to_string($list,array(
            'business_shop_year'=>array('0'=>'否','1'=>'是')
        ));
        $this->assign('_list',$list);
        $this->getActivityList(); //活动列表
        $this->getBusinessCategoryList();//品类列表
        $this->assign('meta_title','店铺管理');
        cookie('__forward__',$_SERVER['REQUEST_URI']);
        $this->display('shop');
    }
    
    /**
     * @todo 招商会我的店铺（我录入的）
     * @author fzh
     */
    public function myShop(){
        $search_text = trim(I('search_text','','strip_tags'));//搜索内容
        $activity_id = (int)I('activity_id');
        $business_category_id = (int)I('business_category_id');
        $map = array();//初始化查询条件
        //查询条件
        if(!empty($search_text)){
            $map['business_shop_name'] = array('like', '%'.(string)$search_text.'%');
        }
        if($activity_id>0){
            $map['activity_id'] = $activity_id;
        }
        if($business_category_id>0){
            $map['business_category_id'] = $business_category_id;
        }
        //列表
        $map['status'] = array('gt',-1);
        $map['uid'] = UID;
        $list = $this->lists('business_shop',$map,'business_shop_id desc');
        int_to_string($list,array(
            'business_shop_year'=>array('0'=>'否','1'=>'是')
        ));
        $this->assign('_list',$list);
        $this->getActivityList(); //活动列表
        $this->getBusinessCategoryList();//品类列表
        $this->assign('meta_title','店铺管理');
        cookie('__forward__',$_SERVER['REQUEST_URI']);
        $this->display('myShop');
    }
    
    /**
     * @todo 添加店铺
     * @author fzh
     */
    public function addShop(){
        if(IS_POST){
            $this->updateShop();
        }else{
            $this->getActivityList(); //活动列表
            $this->getBusinessCategoryList();//品类列表
            $this->assign('list',null);
            $this->assign('meta_title','新增店铺');
            $this->assign('meta_title_c','店铺管理');
            $this->display('addShop');
        }
    }
    
    /**
     * @todo 导入店铺
     * @author fzh
     */
    public function importiShop(){
        if(IS_POST){
            $file_id = (int)I('file_id');
            $activity_id = (int)I('activity_id');
            $business_category_id = (int)I('business_category_id');
            
            //导入
            $obj = D('BusinessShop');
            $result = $obj->importiShop($file_id,$activity_id,$business_category_id);
            //返回值
            if($result === false){
                $json_data['status'] = 0;
                $json_data['info'] = $obj->getError();
                $this->ajaxReturn($json_data);
            }else{
                action_log('business_shop_import', 'business_shop', 0, UID);
                $json_data['status'] = 1;
                $json_data['data'] = $result;
                $this->ajaxReturn($json_data);
            }
        }else{
            $this->getActivityList(); //活动列表
            $this->getBusinessCategoryList();//品类列表
            $this->assign('tempurl',C('DOWNLOAD_UPLOAD.rootPath').'File/down1.csv');
            $this->assign('meta_title_c','店铺管理');
            $this->assign('meta_title','导入店铺');
            $this->display();
        }
    }
    
    /**
     * @todo 编辑店铺
     * @author fzh
     */
    public function editShop(){
        if(IS_POST){
            $this->updateShop();
        }else{
            $id = (int)I('id');
            if($id == 0){
                $this->error('店铺id错误');
            }
            $list = D('BusinessShop')->businessShopShow($id);
            if(empty($list)){
                $this->error('店铺信息不存在');
            }
            $this->getActivityList(); //活动列表
            $this->getBusinessCategoryList();//品类列表
            $this->assign('list',$list);
            $this->assign('meta_title','更新店铺');
            $this->assign('meta_title_c','店铺管理');
            $this->display('addShop');
        }
    }
    
    /**
     * @todo 店铺状态修改
     * @param string $method 标识
     * @author fzh
     */
    public function changeShopStatus($method=null){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if(empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $map[D('BusinessShop')->getPk()] = array('in',$id);
        switch ( strtolower($method) ){
//             case 'forbiduser':
//                 $this->forbid('BusinessShop', $map );
//                 break;
//             case 'resumeuser':
//                 $this->resume('BusinessShop', $map );
//                 break;
            case 'deleteuser':
                action_log('business_shop_del', 'business_shop', $id, UID);
                $this->delete('BusinessShop', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }
    
    /**
     * @todo 添加/编辑店铺(仅供内部调用)
     * @author fzh
     */
    private function updateShop(){
        $business_shop_id = (int)I('business_shop_id');
        $obj = D('BusinessShop');
        $id = $obj->update();
        if(empty($id)){
            $this->error($obj->getError());
        }
        if($business_shop_id == 0){
            action_log('business_shop_add', 'business_shop', $id, UID);
        }else{
            action_log('business_shop_edit', 'business_shop', $business_shop_id, UID);
        }
        $this->success('操作成功',cookie('__forward__'));
    }
    
    /**
     * @todo 品类列表
     * @author fzh
     */
    private function getBusinessCategoryList(){
        $business_category_list = D('BusinessCategory')->getBusinessCategoryList(array('status'=>array('gt',-1)));
        $this->assign('business_category_list',$business_category_list);
    }
    
    /**
     * @todo 活动列表
     * @author fzh
     */
    private function getActivityList(){
        $activity_list = D('Activity','Service')->getActivityList(0);//活动列表
        $this->assign('activity_list',$activity_list);
    }
}